import os
class Utils:
    def __init__(self):
        pass
    # 获取目录下所有文件夹
    def get_dir_dir(self, path,has_child=False):
        """
        :param path: 路径
        :param has_child: 是否递归
        :return:
        """
        narray =[]
        for item in os.listdir(path):
            if os.path.isdir(os.path.join(path, item)) and item != "__pycache__":
                if has_child:
                    tem = self.get_dir_dir(os.path.join(path, item),has_child)
                    narray.extend(tem)
                narray.append(item)
        return narray
    def get_dir_file(self, path,has_child=False):
        '''

        :param path: 路径
        :param has_child:是否递归
        :return:
        '''
        narray =[]
        for item in os.listdir(path):
            if os.path.isdir(os.path.join(path, item)) and item != "__pycache__":
                if has_child:
                    tem = self.get_dir_file(os.path.join(path, item),has_child)
                    narray.extend(tem)
                else:
                    pass
            elif os.path.isfile(os.path.join(path, item)) and item != "__pycache__" and item != "__init__.py":
                narray.append(item)
        return narray

    def get_url_2_module(self,url,base_url=''):
        '''
        将url转换为模块名
        :param url:
        :return:
        '''
        url = str(url)
        base_url = str(base_url)
        if base_url:
            url = url.replace(base_url,"")
        else:
            url = url
        url = url.lstrip("/")
        module_arr = url.split("/")
        print(module_arr)
        if len(module_arr)>0:
            if module_arr[0]:
                module = module_arr[0]
            else:
                module = "index"
        else:
            module = "index"
        return module
    def get_url_2_controller(self,url,base_url=''):
        '''
        将url转换为控制器名
        :param url:
        :return:
        '''
        url = str(url)
        base_url = str(base_url)
        if base_url:
            url = url.replace(base_url,"")
        else:
            url = url
        url = url.lstrip("/")
        module_arr = url.split("/")
        if len(module_arr)>1:
            if module_arr[1]:
                controller = module_arr[1]
            else:
                controller = "index"
        else:
            controller = "index"
        return controller
    def get_url_2_action(self,url,base_url=''):
        '''
        将url转换为控制器名
        :param url:
        :return:
        '''
        url = str(url)
        base_url = str(base_url)
        if base_url:
            url = url.replace(base_url,"")
        else:
            url = url
        url = url.lstrip("/")
        module_arr = url.split("/")
        if len(module_arr)>2:
            if module_arr[2]:
                action = module_arr[2]
            else:
                action = "index"
        else:
            action = "index"
        return action